/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* SdProjectTaskFeedbackService
* 项目任务反馈
*
* 修改纪录
* 2021-12-21 版本：1.0 hdy 创建。
* @version 版本：1.0
* @author 作者：hdy
* 创建日期：2021-01-14
*/
package com.topscomm.gpm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.topscomm.gpm.service.IGpmProjectTaskFeedbackService;
import com.topscomm.gpm.service.impl.auto.GpmProjectTaskFeedbackServiceAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.FlowDatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.gpm.pojo.GpmProjectTaskFeedbackEntity;

@Service(value = "gpmProjectTaskFeedbackService")
@Scope(value = "singleton")
public class GpmProjectTaskFeedbackService extends GpmProjectTaskFeedbackServiceAuto
		implements IGpmProjectTaskFeedbackService {
	FlowDatacodeCache flowDatacodeCache = new FlowDatacodeCache();
	protected List<Map<String, Object>> flowidList = new ArrayList();

	/**
	 * @author: hdy
	 * @date: 2021-12-21
	 * @param entity
	 * @throws ServiceException
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity) {
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(GpmProjectTaskFeedbackEntity.FieldDocno, docNo);
		}
	}

	/**
	 * @Title: beforeSubmit
	 * @description:
	 * @param entity
	 * @param sessionUserBean
	 * @author: hdy
	 * @date: 2021年12月21日
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(TapEntity entity, SessionUserBean sessionUserBean) {
		super.beforeSubmit(entity, sessionUserBean);
		GpmProjectTaskFeedbackEntity gpmProjectTaskFeedbackEntity = (GpmProjectTaskFeedbackEntity) entity;
		this.flowidList = flowDatacodeCache.getApprovalFlowListMap("ApprovalFlow_GpmProjectTaskFeedback",
				gpmProjectTaskFeedbackEntity.getApplyusercode(), "sortcode");
		if (flowidList.size() > 0) {// 赋值审批流程id
			gpmProjectTaskFeedbackEntity.setFlowid(ConvertUtil.convertToLong(flowidList.get(0).get("id")));
		} else {
			throw new ServiceException("没有匹配的审批流，请联系管理员！");
		}
		// 判断是否有审批中的单据，有则不让提交
		String taskid = gpmProjectTaskFeedbackEntity.getTaskid();
		long id = gpmProjectTaskFeedbackEntity.getId();
		StringBufferProxy whereSql = new StringBufferProxy();
		whereSql.appendLineSingle(" taskid='{0}' and id != {1} and state = 1", taskid,id);
		List<GpmProjectTaskFeedbackEntity> sdProjectTaskFeedbackEntities = this.queryByWhere(whereSql.toString());
		if(sdProjectTaskFeedbackEntities.size()>0) {
			throw new ServiceException("存在仍在审核中的“进度更新”单据，请等待审核完毕后再新增“进度更新”单据！进度详情可点击历史进度查看。");
		}
	}
}
